package com.yoo.ons.function.home

import android.os.Build
import android.view.ViewGroup
import androidx.annotation.RequiresApi
import androidx.recyclerview.widget.RecyclerView
import com.yoo.ons.R
import com.yoo.ons.base.adpater.BasePagedAdapter
import com.yoo.ons.base.adpater.ErrorPlaceHolderVH
import com.yoo.ons.base.adpater.ItemType.EmptyPlaceHolder
import com.yoo.ons.base.adpater.ItemType.ErrorPlaceHolder
import com.yoo.ons.base.adpater.ItemType.NoMorePlaceHolder
import com.yoo.ons.base.adpater.NoMoreVH
import com.yoo.ons.event.MessageEvent
import com.yoo.ons.function.home.viewmodel.ItemType
import com.yoo.ons.model.HomeListModel
import com.yoo.ons.util.inflate
import org.greenrobot.eventbus.EventBus

class HomeDataListAdapter : BasePagedAdapter() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            ItemType.HomeListData -> HomeDataListViewHolder(parent, callback)
            ErrorPlaceHolder -> HomeErrorViewHolder(parent, callback)
            NoMorePlaceHolder,EmptyPlaceHolder -> NoMoreVH(parent.inflate(R.layout.layout_empty))
            else -> super.onCreateViewHolder(parent, viewType)
        }
    }

    @RequiresApi(Build.VERSION_CODES.S)
    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val item = getItem(position)
        when (holder) {
            is HomeDataListViewHolder -> item?.data<HomeListModel.MatchIndexBean>()
                ?.let { holder.bind(it, position) }
            is NoMoreVH -> {
                //如果出现这个告诉首页去禁用划卡
                EventBus.getDefault().post(MessageEvent(3,""))
            }
        }
    }

    //获取当前卡片bean
    fun getCardItem(position: Int): HomeListModel.MatchIndexBean? {
        return getItem(position)?.data<HomeListModel.MatchIndexBean>()
    }
    //切换banner
    fun setBannerCurrentItem(holder: RecyclerView.ViewHolder,type:Int){
        if(holder is HomeDataListViewHolder){
            holder.setBannerCurrentItem(type)
        }
    }
    //点击显示信息弹窗隐藏底部内容以及按钮
    fun hideCardContentUI(holder: RecyclerView.ViewHolder,isShow:Boolean){
        if(holder is HomeDataListViewHolder){
            holder.hideCardContentUI(isShow)
        }
    }
}